<?php

namespace Blog\Model;

use Hx\Mvc\Model\Table;

class Blog extends Table {

    public function __construct() {
        $this->setTable('fxh_blog');
    }

    public function getHot($size = 6) {
        return $this->select(array(
            'limit' => $size,
            'order' => 'commentNum DESC',
            'where' => 'type = "blog" AND status = "publish" AND commentNum > 0'
        ), 'id,title,slug,commentNum');
    }

    private function _list($option, $page, $size) {
        $data['blog'] = $this->select($option, 'id,title,slug,desc,createTime,commentNum,categoryId,userId');
        $count = (int)$this->count($option);
        $pages = (int)($count / $size + 0.99);
        $data['count'] = array(
            'pages'   => $pages,
            'all'     => $count,
            'begin'   => $page == 1 ? 1 : ($page - 1) * $size,
            'end'     => ($page * $size) > $count ? $count : ($page * $size),
            'current' => $page
        );
        return $data;
    }

    public function getList($page, $size) {
        $option = array(
            'page'  => $page . ',' . $size,
            'order' => 'id DESC',
            'where' => 'status = "publish"'
        );
        return $this->_list($option, $page, $size);
    }

    public function getCategoryList($id, $page, $size) {
        $option = array(
            'page'  => $page . ',' . $size,
            'order' => 'id DESC',
            'where' => 'status = "publish" AND categoryId = :id',
            'param' => array(
                'id' => $id
            )
        );
        return $this->_list($option, $page, $size);
    }

    public function getUserList($id, $page, $size) {
        $option = array(
            'page'  => $page . ',' . $size,
            'order' => 'id DESC',
            'where' => 'status = "publish" AND userId = :id',
            'param' => array(
                'id' => $id
            )
        );
        return $this->_list($option, $page, $size);
    }

    public function getAll($month = TRUE) {
        $blog = $this->select(array(
            'order' => 'createTime DESC,id DESC',
            'where' => 'type = "blog" AND status = "publish"'
        ), 'id,title,slug,commentNum,createTime');
        if (!$month) {
            return $blog;
        }
        foreach ($blog as $k => $v) {
            $blog[date('Y.m', $v->createTime)][] = $v;
            unset($blog[$k]);
        }
        return $blog;
    }

    public function getById($id) {
        $blog = $this->only(array(
            'where' => 'id = :id AND status = "publish"',
            'param' => array('id' => $id)
        ), 'id,title,slug,content,createTime,commentNum,categoryId,userId,isComment');
        if (!$blog) {
            return NULL;
        }
        if ($blog->categoryId > 0) {
            $sql = $this->useSql('id,name,slug', 'fxh_category')
                ->where('id = :id')
                ->select();
            $blog->category = $this->db->exec($sql, array('id' => $blog->categoryId))->fetch();
        }
        $sql = $this->useSql('id,user,nickName', 'fxh_user')
            ->where('id = :id')
            ->select();
        $blog->user = $this->db->exec($sql, array('id' => $blog->userId))->fetch();
        return $blog;
    }

    public function saveOne($data) {
        if ($data['id'] > 0) {
            return $this->editOne($data, $data['id']);
        }
        return $this->createOne($data);
    }


    private function hasSlug($slug, $id) {
        $data = $this->only(array(
            'where' => 'slug = :slug',
            'param' => array('slug' => $slug)
        ), 'id');
        if (!$data) {
            return FALSE;
        }
        if ($id) {
            if ($id == $data->id) {
                return FALSE;
            }
        }
        return TRUE;
    }

    private function createOne($data) {
        if ($this->hasSlug($data['slug'], 0)) {
            return FALSE;
        }
        unset($data['id']);
        $data['desc'] = substr($data['content'], 0, stripos($data['content'], '<hr class="blog-break" />'));
        $data['createTime'] = time();
        $data['editTime'] = $data['createTime'];
        $data['desc'] = htmlspecialchars($data['desc']);
        $data['content'] = htmlspecialchars($data['content']);
        $data['isComment'] = (int)$data['isComment'];
        $data['isFeed'] = (int)$data['isFeed'];
        $this->insert($data);
        $data['id'] = $this->db->lastId();
        return $data;
    }

    private function editOne($data, $id) {
        if ($this->hasSlug($data['slug'], $id)) {
            return FALSE;
        }
        unset($data['id']);
        $data['desc'] = substr($data['content'], 0, stripos($data['content'], '<hr class="blog-break" />'));
        $data['editTime'] = time();
        $data['desc'] = htmlspecialchars($data['desc']);
        $data['content'] = htmlspecialchars($data['content']);
        $data['isComment'] = (int)$data['isComment'];
        $data['isFeed'] = (int)$data['isFeed'];
        if (!$data['status']) {
            $data['status'] = 'draft';
        }
        $this->update($data, array(
            'where' => 'id = :id',
            'param' => array('id' => $id)
        ));
        $data['id'] = $id;
        return $data;
    }

    //-------------

    public function changeStatus($id, $status) {
        $data = array();
        $data['status'] = $status;
        $this->update($data, array(
            'where' => 'id = :id',
            'param' => array('id' => $id)
        ));
        return $status;
    }

}